<?php
namespace Yao\Controller;
use Common\Controller\YaoController;
class RockController extends YaoController {

    public function get_config()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if (! empty($input_data['app']) && ! empty($input_data['version']))
        {
            //跟多好玩
            $extra['app_id']    =   'wxd448bc6c686baf92';
            $extra['path']      =   'pages/index';
            $more['app_id']     =   'wxd448bc6c686baf92';
            $more['extra']      =   $extra;
            $more['path']       =   'pages/index';

            //分享
            $share['img']       =   'https://qphone.mamapai.net/html/dou_share.png';
            $share['mine']      =   '快，抖抖摇音乐！一起抖起来！';

            $data           =   array();
            $data['jump']   =   array();
            $data['key']    =   'EKMOWTUV3CCLErZg';
            $data['more']   =   $more;
            $data['rule']   =   array();
            $data['share']  =   $share;
            $data['version']=   100;

            $this->returnData($data,'ok',0);
        }
    }

    //(完成)
    public function get_user_group()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if (! empty($input_data['app']) && ! empty($input_data['version']) && ! empty($input_data['token']))
        {
            $data   =   array();
            $where['token'] =   $input_data['token'];
            $field          =   array('yao_member_id');
            $member_data    =   M('yao_member')->where($where)->field($field)->find();
            if (! empty($member_data ))
            {
                $where_group_member             =   array();
                $field_group_member             =   array('id','group_id');
                $where_group_member['user_id']  =   $member_data['yao_member_id'];
                $group_member   =   M('yao_group_member')->where($where_group_member)->field($field_group_member)->select();
                if (! empty($group_member))
                {
                    $where_group        =   array();
                    $list_group_id      =   array();
                    $field_group        =   array('id','group_name','group_id','create_time','update_time');
                    foreach ($group_member as $val)
                    {
                        $list_group_id[]    =   $val['group_id'];
                    }
                    $where_group['group_id']  = array('between',$list_group_id);

                    $group_member   =   M('yao_group')->where($where_group)->field($field_group)->select();

                    if (! empty($group_member))
                    {
                        foreach ($group_member as $vals)
                        {
                            $group_val      =   array();
                            $group_val['group_id']  =   $vals['group_id'];
                            $group_val['group_name']['create_time'] =   $vals['create_time'];
                            $group_val['group_name']['group_id']    =   $vals['group_id'];
                            $group_val['group_name']['id']          =   $vals['id'];
                            $group_val['group_name']['name']        =   $vals['group_name'];
                            $group_val['group_name']['update_time'] =   $vals['update_time'];
                            $data[]         =  $group_val;
                        }
                    }

                }

                $this->returnData($data,'ok',0);
            }
            else
            {
                $this->returnData('','please login',101);
            }
        }

        $data   =   array();
        $this->returnData($data,'参数错误',1001);
    }

    //音乐
    public function music_list()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);
        if (! empty($input_data['app']) && ! empty($input_data['version']))
        {
            $str = '{"code":0,"msg":"ok","data":[{"id":"1","title":"一人我饮酒醉","url":"http://oss.beikg.cn/appmusic/yao/yryjz.mp3"},{"id":"2","title":"浙江温州江南皮革厂倒闭了","url":"http://oss.beikg.cn/appmusic/yao/wzpxdbl.mp3"},{"id":"3","title":"大力哥浑身难受Rap","url":"http://oss.beikg.cn/appmusic/yao/dghsns.mp3"},{"id":"4","title":"Are You OK","url":"http://oss.beikg.cn/appmusic/yao/Are_You_OK.mp3"},{"id":"5","title":"杀马特遇见洗剪吹","url":"http://oss.beikg.cn/appmusic/yao/smtyjxjc.mp3"},{"id":"6","title":"挖掘机技术哪家强","url":"http://oss.beikg.cn/appmusic/yao/wjjjjnjq.mp3"},{"id":"7","title":"王者荣耀rap","url":"http://oss.beikg.cn/appmusic/yao/hzry.mp3"},{"id":"8","title":"我的核爆神曲aDIAOz","url":"http://oss.beikg.cn/appmusic/yao/wdhbsq.mp3"},{"id":"9","title":"我的滑板鞋","url":"http://oss.beikg.cn/appmusic/yao/wdhbx.mp3"},{"id":"10","title":"友军之围","url":"http://oss.beikg.cn/appmusic/yao/yjzw.mp3"}]}';

            $music_data = json_decode($str, true);
            $data   =   $music_data['data'];
            $this->returnData($data,'ok',0);
        }
    }

    //(完成)
    public function share()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);
        if (! empty($input_data) || $_POST)
        {
            if (! empty($input_data['app']) && ! empty($input_data['version'])
                && ! empty($input_data['token']) && ! empty($input_data['iv'])
                && ! empty($input_data['encryptedData']) && ! empty($input_data['code']))
            {
                $where['token'] =   $input_data['token'];
                $field          =   array('yao_member_id','session_key','openid','nickname','avatar');
                $member_data    =   M('yao_member')->where($where)->field($field)->find();

                if (! empty($member_data))
                {
                    $appid      = 'wxd448bc6c686baf92';
                    $sessionkey = $member_data['session_key'];
                    //解析openGId
                    $openGId = $this->getopenGId($appid,$sessionkey,$input_data['encryptedData'],$input_data['iv']);

                    if ($openGId != -1)
                    {
                        //创建群
                        $where_group['group_id']    =   $openGId;
                        $field                      =   array('group_id');
                        $group_data     =   M('yao_group')->where($where_group)->field($field)->find();

                        if (empty($group_data))
                        {
                            $insert_group   =   array();
                            $insert_group['group_id']       =   $openGId;
                            $insert_group['create_time']    =   time();
                            $insert_group['update_time']    =   time();
                            M('yao_group')->add($insert_group);
                        }

                        //加入群
                        $where_group_member    =   array();
                        $where_group_member['user_id']  =   $member_data['yao_member_id'];
                        $where_group_member['group_id'] =   $openGId;
                        $is_exit    =   M('yao_group_member')->where($where_group_member)->field($field)->find();
                        if (empty($is_exit))
                        {
                            $join_group                 =   array();
                            $join_group['user_id']      =   $member_data['yao_member_id'];
                            $join_group['nickname']     =   $member_data['nickname'];
                            $join_group['avatar']       =   $member_data['avatar'];
                            $join_group['group_id']     =   $openGId;
                            $join_group['create_time']  =   time();
                            M('yao_group_member')->add($join_group);
                        }

                        $data['group_id']   =   $openGId;
                        $this->returnData($data,$msg='ok',$code=0);
                    }
                    else
                    {
                        $this->returnData('','获取群id错误',101);
                    }
                }
                else
                {
                    $this->returnData('','please login',101);
                }
            }
        }

        $data   =   array();
        $this->returnData($data,'参数错误',1001);

    }

    //(完成)
    public function group_info()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);
        if ( (! empty($input_data['app']) && ! empty($input_data['version']))
            && ! empty($input_data['group_id']) && ! empty($input_data['token']))
        {
            $data           =   array();
            $where['token'] =   $input_data['token'];
            $field          =   array('yao_member_id');
            $member_data    =   M('yao_member')->where($where)->field($field)->find();

            if (! empty($member_data))
            {
                //群
                $where_group                =   array();
                $field_group               =   array('id','group_name','group_id','create_time','update_time','total_rock');
                $where_group['group_id']    =   $input_data['group_id'];
                $group_data =   M('yao_group')->where($where_group)->field($field_group)->find();
                if (! empty( $group_data))
                {
                    $data['group_name']                 =   $group_data['group_name'];
                    $data['group_ranking']['ranking']   =   2;
                    $data['group_ranking']['total_rock']=   $group_data['total_rock'];
                }

                //我的群数据
                $where_my_group             =   array();
                $field_my_group             =   array('user_id','rock_num','rock_day','update_time','create_time','group_id','id');
                $where_my_group['group_id'] =   $input_data['group_id'];
                $where_my_group['user_id']  =   $member_data['yao_member_id'];
                $my_group   =   M('yao_group_member')->where($where_my_group)->field($field_my_group)->find();
                if (! empty($my_group))
                {
                    $data['my']['create_time']      =   $my_group['create_time'];
                    $data['my']['group_id']         =   $my_group['group_id'];
                    $data['my']['id']               =   $my_group['id'];
                    $data['my']['rock_day']         =   $my_group['rock_day'];
                    $data['my']['user_id']         =   $my_group['user_id'];
                    $data['my']['update_time']      =   $my_group['update_time'];
                    $data['my']['rock_num']         =   $my_group['rock_num'];
                }

                $this->returnData($data,'ok',0);
            }
            else
            {
                $this->returnData('','please login',101);
            }
        }


        $this->returnData($data,'参数错误',1001);
    }

    //(完成)
    public function add_rock()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);
        if (! empty($input_data))
        {
            if (! empty($input_data['app']) && ! empty($input_data['version'])
                && ! empty($input_data['group_id']) && ! empty($input_data['num'])
                && ! empty($input_data['token']) )
            {
                $where['token'] =   $input_data['token'];
                $field          =   array('yao_member_id');
                $member_data    =   M('yao_member')->where($where)->field($field)->find();
                if (! empty($member_data))
                {
                    $return_data=   array();
                    $today_time =   strtotime(date('Y-m-d'));

                    //更新用户群数据
                    $where_group_member             =   array();
                    $where_group_member['user_id']  =   $member_data['yao_member_id'];
                    $where_group_member['group_id'] =   $input_data['group_id'];
                    $field_group_member     =   array('update_time','rock_num','total_rock_today');
                    $group_member   =   M('yao_group_member')->where($where_group_member)->field($field_group_member)->find();
                    if (! empty($group_member))
                    {
                        $update_group_member                =   array();
                        $update_group_member['rock_num']    =   $group_member['rock_num']   +   $input_data['num'];
                        $update_group_member['update_time'] =   time();
                        if ($group_member['update_time'] >= $today_time)
                        {
                            $update_group_member['total_rock_today']    =   $group_member['total_rock_today'] + $input_data['num'];
                        }
                        else
                        {
                            $update_group_member['rock_day']            =   date('Ymd');
                            $update_group_member['total_rock_today']    =   $input_data['num'];
                        }

                        M('yao_group_member')->where($where_group_member)->save($update_group_member);
                        $return_data['rock_num']    =   $update_group_member['total_rock_today'];
                    }

                    //更新群数据
                    $where_group                =   array();
                    $field_group                =   array('group_id','total_rock','ranking','rock_num','total_rock_today','update_time');
                    $where_group['group_id']    =   $input_data['group_id'];
                    $group_data                 =   M('yao_group')->where($where_group)->field($field_group)->find();
                    if (! empty($group_data))
                    {
                        $update_group               =   array();
                        $update_group['update_time']=   time();
                        $update_group['total_rock'] =   $group_data['total_rock']   +   $input_data['num'];
                        if ($group_data['update_time'] >= $today_time)
                        {
                            $update_group['total_rock_today'] =   $group_data['total_rock_today']   +   $input_data['num'];
                        }
                        else
                        {
                            $update_group['total_rock_today'] =   $input_data['num'];
                        }

                        M('yao_group')->where($where_group)->save($update_group);
                        $return_data['total_rock']  =   $update_group['total_rock_today'];
                    }

                    $this->returnData($return_data,'ok',0);
                }
                else
                {
                    $this->returnData('','please login',101);
                }
            }
        }

        $data   =   array();
        $this->returnData($data,'参数错误',1001);
    }

    //(完成)
    public function update_group_name()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);
        if (! empty($input_data['app']) && ! empty($input_data['version'])
            && ! empty($input_data['group_id']) && ! empty($input_data['name'])
            && ! empty($input_data['token']) )
        {
            $where['token'] =   $input_data['token'];
            $field          =   array('yao_member_id');
            $member_data    =   M('yao_member')->where($where)->field($field)->find();
            if (! empty($member_data))
            {
                $where_group                =   array();
                $update_data                =   array();
                $where_group['group_id']    =   $input_data['group_id'];
                $update_data['group_name']  =   $input_data['name'];
                $update_data['update_time'] =   time();
                M('yao_group')->where($where_group)->save($update_data);
                $data   =   array();
                $this->returnData($data,'ok',0);
            }
            else
            {
                $this->returnData('','please login',101);
            }
        }
        else
        {
            $data   =   array();
            $this->returnData($data,'参数错误',1001);
        }
    }

    //(完成)
    public function group_member()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if (! empty($input_data['app']) && ! empty($input_data['version'])
        && ! empty($input_data['token']) && ! empty($input_data['group_id'])
        && ! empty($input_data['page']) && ! empty($input_data['page_size']))
        {
            $page           =   ($input_data['page'] -1) * $input_data['page_size'];
            $page_size      =   $input_data['page_size'];

            //token验证
            $where['token'] =   $input_data['token'];
            $field          =   array('yao_member_id');
            $member_data    =   M('yao_member')->where($where)->field($field)->find();
            if (empty($member_data))
            {
                $this->returnData('','please login',101);
            }

            $where_my_group             =   array();
            $field_my_group             =   array('user_id','rock_num','rock_day','update_time','create_time','group_id','id','avatar','nickname');
            $where_my_group['group_id'] =   $input_data['group_id'];
            $group_member   =   M('yao_group_member')->where($where_my_group)->field($field_my_group)->limit($page,$page_size)->select();

            $data   =   array();
            if (! empty($group_member))
            {
                foreach ($group_member as $val)
                {
                    $item                       =   array();
                    $item['rock_num']           =   $val['rock_num'];
                    $item['user']['avatar']     =   $val['avatar'];
                    $item['user']['id']         =   $val['user_id'];
                    $item['user']['nickname']   =   $val['nickname'];
                    $item['user_id']            =   $val['user_id'];
                    $data[]                     =   $item;
                }
            }

            $this->returnData($data,'ok',0);
        }

        $this->returnData($data,'参数错误',1001);

    }

    //(完成)
    public function ranking_list()
    {
        $input      = file_get_contents('php://input');
        $input_data = json_decode($input, true);

        if (! empty($input_data['app']) && ! empty($input_data['version'])
            && ! empty($input_data['token']) && ! empty($input_data['page'])
            && ! empty($input_data['page_size']))
        {
            $g_data     =   '{"code":0,"msg":"ok","data":{"list":[{"group_id":"GyD865O_Wavn5mAP9vNjH43RDtjk","total_rock":172429,"group_name":""},{"group_id":"GyD865AYR5IJ5VOGKn6FCFLs5Q78","total_rock":111715,"group_name":""},{"group_id":"GyD865GDddkirgtUNBY6CKZA01CA","total_rock":93015,"group_name":""},{"group_id":"GyD865E8IHSkwHPhmtbNPxJv_0fg","total_rock":89576,"group_name":""},{"group_id":"GyD865Fv4nKTk3Gr0A7_DeFPAri8","total_rock":69495,"group_name":""},{"group_id":"GyD865GwX4ucepRoNkfGphj4_vHM","total_rock":50666,"group_name":"比特熊战队"},{"group_id":"GyD865HV-5Kt8dELcRcBbYBU1Eq8","total_rock":39147,"group_name":""},{"group_id":"GyD865Pf2yNWFt5D3EU96ZrFeUCc","total_rock":38989,"group_name":""},{"group_id":"GyD865HFuvw7tTGZyVWufzg4kXEw","total_rock":36054,"group_name":""},{"group_id":"GyD865JOJ-pAsE7GJaCpLnd3aCKE","total_rock":36010,"group_name":"柠檬不萌"}]}}';
            $group_datas =   json_decode($g_data, true);

            $where_group        =   array();
            $list_group_id      =   array();
            $page               =   ($input_data['page'] - 1) * $input_data['page_size'];
            $page_size          =   $input_data['page_size'];
            $field_group        =   array('group_name','group_id','total_rock');
            $group_data         =   M('yao_group')->field($field_group)->order(array('total_rock'=>'desc'))->limit($page,$page_size)->select();

            if ($input_data['page'] == 1)
            {
                $data['list']       =   array_merge($group_datas['data']['list'],$group_data);
            }
            else
            {
                $data['list']       =   $group_data;
            }

            $this->returnData($data,'ok',0);
        }
        else
        {
            $data   =   array();
            $this->returnData($data,'参数错误',1001);
        }
    }

    /**
     * 存放模板消息
     */
    public function send($member_data, $form_id, $form_type = 1)
    {
        $template_id    =   1;
        $date           =   date('Y-m-d');
        $insert = array(
            'appid'=>'wx66da7c3d2bc96fc0',
            'touser'=>$member_data['third'],
            'template_id'=>$template_id,
            'page'=>'pages/index/index',
            'form_id'=>$form_id,
            'form_type'=>$form_type,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>'e1472ac4b86362d3ce31ff5e66899ed9',
            'createtime'=>$date,
            'colour_member_id'=>$member_data['colour_member_id']
        );

        M('colour_template_send')->data($insert)->add();
//        $where['touser']     = $member_data['third'];
//        $where['createtime'] = $date;
//        $where['status']     = '-1';
//        $ishave = M('colour_template_send')->field('wxopen_template_send_id')->where($where)->find();
//        if ($ishave)
//        {
//            M('colour_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
//        }
//        else
//        {
//            M('colour_template_send')->data($insert)->add();
//        }
    }

}